package com.foreveross.taskservice.application.impl.task.service;

import java.util.Date;
import java.util.List;

import org.dayatang.domain.CriteriaQuery;
import org.dayatang.domain.JpqlQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.taskservice.application.impl.BaseJpaApplicationImpl;
import com.foreveross.taskservice.application.task.service.ITaskTemplateApplication;
import com.foreveross.taskservice.common.GlobalsConst;
import com.foreveross.taskservice.core.domain.task.TaskLine;
import com.foreveross.taskservice.core.domain.task.TaskTemplateEntity;
import com.google.common.collect.Lists;

@Service
@Transactional
public class TaskTemplateApplicationImpl extends BaseJpaApplicationImpl implements ITaskTemplateApplication{
	@Override
	public List<TaskLine> getAllLines() throws Exception {
		JpqlQuery jq=this.createJpqlQuery("select t.fromCity,t.toCity " +
				"from TaskTemplateEntity as t group by t.fromCity,t.toCity,t.priority order by t.priority desc");
		List<Object[]> result=this.find(jq);
		List<TaskLine> lines=Lists.newArrayList();
		if(result!=null && !result.isEmpty()){
			for(Object[] r:result){
				lines.add(new TaskLine((String)r[0], (String)r[1]));
			}
		}
		return lines;
	}


	@Override
	public void updateAllTemplateNextGrabTime(Date nextTime) throws Exception {
		JpqlQuery jq=createJpqlQuery("update TaskTemplateEntity set nextGrabTime=?");
		jq.setParameters(nextTime);
		executeUpdate(jq);
	}
	@Override
	public void updateChannelTemplateNextGrabTime(Date nextTime,String channelId)throws Exception{
		JpqlQuery jq=createJpqlQuery("update TaskTemplateEntity set nextGrabTime=? where attachHbaseKey=?");
		jq.setParameters(nextTime,channelId);
		executeUpdate(jq);
	};
	
	@Override
	public List<TaskTemplateEntity> getTaskTemplatesOrderPriorityDesc(
			final int maxSize) throws Exception {
//		final Date d=new Date();
//		return getHibernateTemplate().execute(new HibernateCallback<List<TaskTemplateEntity>>() {
//
//			@SuppressWarnings("unchecked")
//			@Override
//			public List<TaskTemplateEntity> doInHibernate(Session session)
//					throws HibernateException, SQLException {
//				return session.createCriteria(TaskTemplateEntity.class)
//						.add(Restrictions.eq("status", GlobalsConst.DATA_STATUS_NORMAL))
//						.add(Restrictions.le("validityFrom",d))
//						.add(Restrictions.ge("validityTo", d))
//						.add(Restrictions.le("nextGrabTime", d))
//						.add(Restrictions.in("grabChannelId", session.createCriteria(GradChannelEntity.class)
//								.add(Restrictions.eq("status", GlobalsConst.DATA_STATUS_NORMAL))
//								.setProjection(Projections.groupProperty("id")).list()
//							))
//						.addOrder(Order.desc("priority"))
//						.setMaxResults(maxSize).list();
//			}
//		});
		return null;
	}

	@Override
	public List<TaskTemplateEntity> getTaskTemplatesOrderPriorityDesc(
			final int templateMaxSize, final String hbaseKey) throws Exception {
		final Date d=new Date();
		return createCriteriaQuery(TaskTemplateEntity.class)
			.eq("status", GlobalsConst.DATA_STATUS_NORMAL)
			.le("validityFrom", d)
			.ge("validityTo", d)
			.le("nextGrabTime", d)
			.eq("attachHbaseKey", hbaseKey)
			.desc("priority").setMaxResults(templateMaxSize).list();
	}



	@Override
	public List<TaskTemplateEntity> getTaskTemplatesGreaterThanPriority(
			final int priority,final String hbaseKey) throws Exception {
		final Date d=new Date();
		CriteriaQuery cq=createCriteriaQuery(TaskTemplateEntity.class)
			.eq("status", GlobalsConst.DATA_STATUS_NORMAL)
			.le("validityFrom", d)
			.ge("validityTo", d)
			.le("nextGrabTime", d)
			.gt("priority", priority)
			.eq("attachHbaseKey", hbaseKey);
		return find(cq);
	}

	
	
	
}
